<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- swipeable 滚动  animated 动画 active = 0 默认选中为下标为0-->
    <van-tabs v-model="active" swipeable animated class="channel-tabs">
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 此时传过去的是channels的每一项 -->
        <ArticleList :channel="item"></ArticleList>
      </van-tab>
      <!-- 占位 -->
      <div slot="nav-right" class="placeholder"></div>
      <!-- 汉堡按钮 -->
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <channelEdit
        :myChannels="channels"
        :myActive="active"
        @update-active="onUpdateActive"
      ></channelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/api/user";
import ArticleList from "../home/components/articleList.vue";
import channelEdit from "./components/channel-edit.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage.js";

export default {
  data() {
    return {
      active: 0,
      channels: [], //我的频道数据
      isChannelEditShow: false,
    };
  },
  computed: {
    ...mapState(['user']),
  },
  created() {
    this.loadChannels();
  },
  methods: {
    // 获取频道列表
    async loadChannels() {
      let channels = [];
      if (this.user) {
        // 已登录，请求获取用户频道列表
        const { data } = await getUserChannels();
        console.log('获取用户频道列表成功');
        channels = data.data.channels;
      } else {
        // 未登录，获取本地的频道列表数据
        const localChannels = getItem("TOUTIAO_CHANNELS");
        // 如果存在本地的频道列表数据
        if (localChannels) {
          channels = localChannels;
        } else {
          // 没有登录还没有本地缓存，请求获取默认频道列表
          const { data } = await getUserChannels();
          channels = data.data.channels;
        }
      }
      this.channels = channels;
    },
    onUpdateActive(index, isChannelEditShow = true) {
      this.active = index;
      this.isChannelEditShow = isChannelEditShow;
    },
  },
  components: {
    ArticleList,
    channelEdit,
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  .van-tab--active {
    color: #5babfb;
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      width: 200px;
      font-size: 30px;
      color: #777777;
    }
    .van-tabs__wrap {
      height: 82px;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background-color: #5babfb;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.9;
      i {
        font-size: 33px;
      }
    }
    .placeholder {
      width: 50px;
      height: 82px;
      flex-shrink: 0;
    }
  }
  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
    height: 82px;
  }
}
</style>